<template>
  <div class="add-main">
    <div class="add-btn flex jc-sb">
      <div v-if="objdata == ''" class="xinxzneg">新增</div>
      <div v-else class="xinxzneg">编辑</div>
      <div class="back flex jc-c ai-c cursor" @click="back()">
        <i class="el-icon-back"></i>
        <span>返回</span>
      </div>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="add-content"
      v-loading="loading">
      <el-form-item label="通知标题" prop="title">
        <el-input style="width: 400px" v-model="ruleForm.title" maxlength="17" :show-word-limit="true"></el-input>
      </el-form-item>
      <el-form-item label="所在端" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择类型">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="内容" prop="imgUrl">
        <v-WangEditor v-model="editorHtml" :is-clear="isClear" :max-height="maxHeight" @on-change="onChange" />
      </el-form-item>
      <el-form-item>
        <el-button class="sure-btn" @click="submitForm('ruleForm')">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { adminNoticeSave, adminNoticeUpdate, adminNoticeInfo } from "@/api/index";
import uploadImg from "@/component/uploadImg/index.vue";
import WangEditor from '@/component/wangeditor'
export default {
  components: {
    "v-uploadImg": uploadImg,
    "v-WangEditor": WangEditor
  },
  props: ["objdata"],
  data() {
    return {
      editorHtml: '',
      isClear: false,
      maxHeight: '',
      loading: false,
      options: [
        {
          value: 0,
          label: "全部",
        },
        {
          value: 1,
          label: "pc端",
        },
        {
          value: 2,
          label: "移动端",
        },
      ],
      ruleForm: {},
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        type: [
          { required: true, message: "请选择类型", trigger: "change" },
        ],
      },
      isedit: false,
    };
  },
  created() {
    if (this.objdata) {
      console.log(this.objdata)
      this.isedit = true;
      this.loading = true
      adminNoticeInfo({
        id: this.objdata
      }).then(res => {
        this.ruleForm = res.data
        this.editorHtml = res.data.content
        this.loading = false
      })
    }
  },
  methods: {
    // 富文本改变
    onChange(val) {
      this.ruleForm.content = val
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid && !this.loading) {
          this.loading = true;
          if (this.isedit) {
            this.adminNoticeUpdate();
          } else {
            this.adminNoticeSave();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ruleForm.content = "";
    },
    adminNoticeUpdate() {
      adminNoticeUpdate(this.ruleForm)
        .then((res) => {
          this.$message.success("编辑成功");
          this.loading = false;
          this.back()
        })
        .catch((res) => {
          this.loading = false;
        });
    },
    adminNoticeSave() {
      adminNoticeSave(this.ruleForm)
        .then((res) => {
          this.$message.success("添加成功");
          this.loading = false;
          this.back()
        })
        .catch((res) => {
          this.loading = false;
        });
    },
    back() {
      this.$emit("close");
      //   this.$router.push({
      //     path: "/Rotationsettings",
      //   });
    },

    handleRemove(file) {
      console.log(file);

      this.picShow = false;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(file) {
      console.log(file);
    },
  },
  mounted() {
    document
      .querySelector("body")
      .setAttribute("style", "background-color:#f2f2f2;");
  },

  beforeDestroy() {
    document.querySelector("body").removeAttribute("style");
  },
};
</script>

<style lang="scss" scoped>
.add {
  width: 100%;
}

.add-main {
  background-color: white;

  .add-btn {
    height: 40px;

    .xinxzneg {
      font-size: 18px;
      font-family: Source Han Sans CN;
      font-weight: 500;
      color: #2f3034;
    }

    .back {
      width: 96px;
      height: 40px;
      background: #ffffff;
      border: 1px solid #e5e5e5;
      border-radius: 4px;
    }

    .el-icon-back {
      color: #60636b;
      margin-right: 6px;
    }
  }

  .add-content {
    padding-top: 70px;
  }

  .add-name {
    display: flex;
    padding-top: 25px;
    padding-left: 70px;

    .add-user {
      margin-right: 17px;
      margin-top: 7px;

      span {
        display: inline-block;
        padding-top: 1px;
        font-size: 14px;
      }
    }

    .add-input {
      width: 414px;

      input {
        width: 414px;
        padding-top: 3px;
        padding-bottom: 3px;
        padding-left: 5px;
        outline: none;
        border: 1px solid rgba(215, 215, 215, 1);
        border-radius: 3px;
      }
    }

    .add-select {
      width: 414px;

      input {
        width: 414px;

        padding-left: 5px;
        outline: none;
        border: 1px solid rgba(215, 215, 215, 1);
        border-radius: 3px;
      }
    }
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>

<style scoped>
/* 确定按钮背景颜色修改 */
.sure-btn {
  width: 96px;
  height: 40px;
  color: #ffffff;
  text-align: center;

  border: none;
  background-color: #ff5478;
  /* 不支持线性的时候显示 */


  align-items: center;
}
</style>